package com.happyrecyclingstation.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.happyrecyclingstation.dto.RegisterDto;
import com.happyrecyclingstation.entity.User;
import com.happyrecyclingstation.mapper.RegisterMapper;
import com.happyrecyclingstation.mapper.UserMapper;
import com.happyrecyclingstation.service.RegisterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param registerDto
     */
    @Override
    public void register(RegisterDto registerDto) {
        User user = new User();
        // 拷贝属性
        BeanUtils.copyProperties(registerDto, user);
        // 将密码进行BCrypt加密
        user.setPassword(BCrypt.hashpw(registerDto.getPassword()));
        // 设置创建时间和修改时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insert(user);
    }
}
